/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package consys.social.core.service;

import consys.social.core.NotAuthorizedException;
import consys.social.core.bo.Entity;
import consys.social.core.bo.Wall;
import consys.social.core.bo.WallPost;
import consys.social.core.dao.WallDao;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 *
 * @author petan
 */
public class WallServiceImpl implements WallService {

    private FollowService followService;
    private WallDao wallDao;
    private List<PostParser> parsers;

    public void setFollowService(FollowService followService) {
        this.followService = followService;
    }

    public void setWallDao(WallDao wallDao) {
        this.wallDao = wallDao;
    }

    @Override
    public void postPost(WallPost post) throws IOException, NotAuthorizedException {
        Entity source = AuthenticatedUser.getUser();
        if (post == null) {
            throw new IllegalArgumentException("Null post");
        }
        if (source == null) {
            throw new IllegalArgumentException("Null entity");
        }
        if (!source.equals(post.getAddedBy())) {
            throw new NotAuthorizedException("You can post only on your wall.");
        }
        if (parsers != null) {
            for (PostParser p : parsers) {
                post = p.parsePost(post);
            }
        }
        List<String> followers = followService.getAllFollowersUid(post.getAddedBy());
        wallDao.savePost(followers, post);
    }

    @Override
    public void deletePost(WallPost post) throws IOException, NotAuthorizedException {
        Entity source = AuthenticatedUser.getUser();
        if (post == null) {
            throw new IllegalArgumentException("Null post");
        }
        if (source == null) {
            throw new IllegalArgumentException("Null entity");
        }
        if (!source.equals(post.getAddedBy())) {
            throw new NotAuthorizedException("You can delete only from your wall.");
        }
        wallDao.deletePost(post);
    }

    @Override
    public Wall loadEntityWall(Entity target, int count, int from) throws IOException, NotAuthorizedException {
        Entity source = AuthenticatedUser.getUser();
        if (source == null) {
            throw new IllegalArgumentException("Null source entity");
        }
        if (target == null) {
            throw new IllegalArgumentException("Null target entity");
        }
        if (!source.equals(target)) {
            throw new NotAuthorizedException("You can view only your wall.");
        }
        return wallDao.loadWall(target, count, from);
    }

    @Override
    public Wall loadEntityWallNewest(Entity target, int count) throws IOException, NotAuthorizedException {
        return loadEntityWall(target, count, 0);
    }

    @Override
    public Wall loadEntityProfileWall(Entity target, int count, Date from) throws IOException, NotAuthorizedException {
        return loadEntityProfileWall(target, count, from, false);
    }

    private Wall loadEntityProfileWall(Entity target, int count, Date from, boolean newest) throws IOException, NotAuthorizedException {
        Entity source = AuthenticatedUser.getUser();
        if (source == null) {
            throw new IllegalArgumentException("Null source entity");
        }
        if (target == null) {
            throw new IllegalArgumentException("Null target entity");
        }
        if (!followService.isFollowing(source, target)) {
            throw new NotAuthorizedException("You can view profile of those who you follow (or yourself).");
        }
        return wallDao.loadProfileWall(target, count, from, newest);
    }

    @Override
    public Wall loadEntityProfileWallNewest(Entity target, int count) throws IOException, NotAuthorizedException {
        return loadEntityProfileWall(target, count, new Date(), true);
    }

    @Override
    public Wall loadWallContainingTag(String string, int count, Date from) throws IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
